package com.netease.nim.demo.common.ui.imageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.netease.nim.demo.common.util.media.BitmapDecoder;
import com.netease.nim.demo.contact.cache.ContactDataCache;
import com.shwread.android.qysw10000182.R;

/**
 * 圆形头像控件
 * <p/>
 * Created by huangjun on 2015/3/10.
 */
public class HeadImageView extends RecyclingImageView {

    public HeadImageView(Context context) {
        super(context);
    }

    public HeadImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private static final int[] headIconResources = new int[]{
            R.drawable.avatar_def, R.drawable.head_icon_1, R.drawable.head_icon_2, R.drawable.head_icon_3,
            R.drawable.head_icon_4, R.drawable.head_icon_5
    };

    /**
     * 解决ViewHolder复用问题
     */
    public void resetImageView() {
        mask = null;
        cover = null;
        setImageBitmap(null);
    }

    public void loadBuddyHeadImage(String account) {
        int index = ContactDataCache.getInstance().getBuddyIcon(account);
        if (index < 0 || index > 5) {
            index = 0;
        }
        int srcId = headIconResources[index];
        int maskId = R.drawable.avatar_mask_100x100;
        int coverId = 0;
        loadResourceImage(srcId, maskId, coverId);
    }

    public void loadResourceHeadImage(int srcId) {
        int maskId = R.drawable.avatar_mask_100x100;
        int coverId = 0;
        loadResourceImage(srcId, maskId, coverId);
    }

    public void loadResourceImage(int srcId, int maskId, int coverId) {
        int width = getWidth();
        int height = getHeight();

        mask = maskId != 0 ? getResources().getDrawable(maskId) : null;
        cover = coverId != 0 ? getResources().getDrawable(coverId) : null;

        Bitmap bmp = BitmapDecoder.decodeSampled(getResources(), srcId, width, height);
        setImageBitmap(bmp);
    }

    private Drawable mask;

    private Drawable cover;

    private static final Paint paintMaskSrcIn = createMaskPaint();

    private static final Paint createMaskPaint() {
        Paint paint = new Paint();

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        return paint;
    }

    private static final Paint paintCoverSrcOver = createCoverPaint();

    private static final Paint createCoverPaint() {
        Paint paint = new Paint();

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mask != null || cover != null) {
            // bounds
            int width = getWidth();
            int height = getHeight();

            // mask
            if (mask != null) {
                canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
                mask.setBounds(0, 0, width, height);
                mask.draw(canvas);
            }

            // mask ∩ source => merge
            canvas.saveLayer(0, 0, width, height, paintMaskSrcIn, Canvas.ALL_SAVE_FLAG);
            super.onDraw(canvas);
            canvas.restore();

            // cover ∪ merge => merge
            if (cover != null) {
                canvas.saveLayer(0, 0, width, height, paintCoverSrcOver, Canvas.ALL_SAVE_FLAG);
                cover.setBounds(0, 0, width, height);
                cover.draw(canvas);
                canvas.restore();
            }

            // apply blend layer
            canvas.restore();
        } else {
            super.onDraw(canvas);
        }
    }
}
